package com.xiaopengpeng.test;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HbaseOperationsImpl implements HbaseOperations{
	private static final Logger logger = LoggerFactory.getLogger(HbaseOperationsImpl.class);
	Connection connection = HbaseConnetcion.getConnection();
	@Override
	public <T> void save(String tableName ,T bean, RowMapper<T> mapper) {
		try {
			
			Table table = connection.getTable(TableName.valueOf(tableName));
			table.put(mapper.mapPut(bean));
		} catch (IOException e) {
			logger.error("", e);
			e.printStackTrace();
		}
		
	}

	@Override
	public void delete(String tableName, String rowkey) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public <T> T findOne(String tableName, String rowkey, RowMapper<T> mapper) {
		try {
			Table table = connection.getTable(TableName.valueOf(tableName));
			Get get = new Get(rowkey.getBytes());
			Result result = table.get(get);
			T mapRow = mapper.mapRow(result);
			return mapRow;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
		
	}

	@Override
	public <T> List<T> findByScan(String tableName, Scan scan, RowMapper<T> mapper) {
		// TODO Auto-generated method stub
		return null;
	}

}
